package edu.office.applicationRepository;

import edu.office.entities.School;
import edu.office.entities.Subject;
import edu.office.entities.TeacherAdmin;
import edu.office.entities.Teacschoolsubject;
import edu.office.tools.Tool;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.time.LocalDate;
import java.util.*;

@Named("teacschoolsubjectRepositoryA")
@ApplicationScoped
public class TeacschoolsubjectRepository extends ApplicationCommonRepository<Teacschoolsubject>{

    @Inject
    SchoolRepository schoolRepository;

    public TeacschoolsubjectRepository() {
    }

//Get the school list that the teacher teaches in this semester
      public Teacschoolsubject getTeacschoolsubject(Integer teacher, Integer school, Integer subject, String aftertime) {
        setDataChanged(false);
        String whereString = "";
        if (null != teacher) {
            whereString += " teacherid=" + teacher;
        }
        if (null != school) {
            whereString += " and schoolid=" + school;
        }
        if (null != subject) {
            whereString += " and subjectid=" + subject;
        }
        if (whereString.startsWith(" and ")) {
            whereString = whereString.substring(4);
        }
        if (!whereString.trim().isEmpty()) {
            whereString += " and totime>'" + aftertime + "'";
        }
        List<Teacschoolsubject> resultList;
        if (whereString.trim().isEmpty()) {
            resultList = find("select * from Teacschoolsubject").list();
        } else {
            resultList = find("select * from Teacschoolsubject where " + whereString).list();
        }
        if (!resultList.isEmpty()) {
            return resultList.get(Tool.getRand(resultList.toArray()));
        } else {
            return null;
        }
    }

    public List<Teacschoolsubject> getTeacschoolsubject(School school, Subject subject) {
        List<Teacschoolsubject> resultList = new LinkedList<>();
        //通过班级school找到所有该学校的其他班的老师，这些老师教同一门课程
        //要求传入的school必须是一个班级，而不是一个学校
        if (!schoolRepository.getSchoolsOnLeaves().contains(school)) {
            return new LinkedList<>();
        }
        //保证了班级，而不是学校,根据班级找学校
        School schoolXuexiao = schoolRepository.find("PARENTID",
                schoolRepository.find("ID",school).firstResult().getPARENTID())
                .firstResult();
        List<School> allSchools =schoolRepository.list("PARENTID",schoolXuexiao.getID());
        //过滤这些班级，过滤方法是：当前时间正在开设这门课程
        for (School school1 : allSchools) {
            Set<Teacschoolsubject> teacschoolsubjects =new HashSet<>(find("SCHOOLID",school1.getID()).list());
            for (Teacschoolsubject teacschoolsubject : teacschoolsubjects) {
                if (teacschoolsubject.getSUBJECTID().equals(subject.getID())) {
                    if (teacschoolsubject.getTOTIME().isAfter(LocalDate.now())) {//当前时间在上课时间在范围内
                        resultList.add(teacschoolsubject);
                    }
                }
            }
        }
        return resultList;

    }

    public Set<Teacschoolsubject> getTeacschoolsubjects4Teacher(TeacherAdmin teacher) {
        //一定不是管理员或秘书，因为他们不用上课
        Set<Teacschoolsubject> resultSet = new HashSet<>();
        List<Teacschoolsubject> teachersTeacschoolsubjects = find("teacher",teacher.getID()).list();
        teachersTeacschoolsubjects.forEach((Teacschoolsubject teaSchoolSubjectiterator) -> {
            if (teaSchoolSubjectiterator.getFROMTIME().isBefore(LocalDate.now()) && teaSchoolSubjectiterator.getTOTIME().isAfter(LocalDate.now())) {
                resultSet.add(teaSchoolSubjectiterator);
            }
        });
        return resultSet;
    }

    public Set<Teacschoolsubject> getTeacschoolsubjects4Subject(Subject subject) {
        //一定不是管理员或秘书，因为他们不用上课
        Set<Teacschoolsubject> resultSet = new HashSet<>();
        List<Teacschoolsubject> teachersTeacschoolsubjects = find("SUBJECTID",subject.getID()).list();
        teachersTeacschoolsubjects.forEach((Teacschoolsubject teaSchoolSubjectiterator) -> {
            if (teaSchoolSubjectiterator.getFROMTIME().isBefore(LocalDate.now()) && teaSchoolSubjectiterator.getTOTIME().isAfter(LocalDate.now())) {
                resultSet.add(teaSchoolSubjectiterator);
            }
        });
        return resultSet;
    }

}
